package sxe.core.ptzctl;

import snobject.snNil;
import snobject.snObject;
import snobject.snObjectArgList;
import step.EvaluationFailure;
import step.FunctionWrapper;
import step.exceptions.ResourceUnavailableException;
import sxe.sensors.GenericSensor;
import sxe.sensors.PTZSensor;
import sxe.sensors.SensorDetector;

public class pan extends FunctionWrapper {

	@Override
	public snObject Call(snObjectArgList argv) throws EvaluationFailure,
			ClassCastException, ArrayIndexOutOfBoundsException,
			ResourceUnavailableException {

		String sSensorID = argv.popString().getString();
		Integer amount = argv.popInteger().getInt();
		
		if((amount >180)||(amount < -180)){
			System.err.println("Specified pan amount " + amount + " exceeds range of [-180..180]");
			throw new EvaluationFailure(this.expnode,"Specified pan amount " + amount + " exceeds range of [-180..180]");
		}

		GenericSensor gs = SensorDetector.GetDevice(sSensorID, GenericSensor.TYPE_IMAGE_PTZ);
		if(gs==null){
			System.err.println("Sensor " + sSensorID + "does not exist or is not of type PTZ");
			throw new EvaluationFailure(this.expnode,"Couldn't get sensor " + sSensorID);
		}
		
		if(! gs.getType().equals(GenericSensor.TYPE_IMAGE_PTZ))
		{
			System.err.println("Sensor " + sSensorID + "is not a PTZ Sesnor");
			throw new EvaluationFailure(this.expnode,"Sensor " + sSensorID + "is not a PTZ Sensor");
		}
		/* grab a frame from the framegrabber */
		snObject snRet = null;
		
		PTZSensor ptz = (PTZSensor) gs; 
		snRet = ptz.pan(amount);
				
		if(snRet.equals(snNil.NIL))
			throw new EvaluationFailure(this.expnode,"Failed to pan camera specified amount.");
		return snRet;

	}

}
